package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsGoods;
import com.jy.wms.pojo.WmsGoodsType;
import com.jy.wms.service.InventoryStatisticsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/statistics")
@Api(value = "/statistics", description = "关于报表的统计_Jane")
public class StatisticsController extends BaseController<InventoryStatisticsService,InventoryStatistics> {

    @ApiOperation(value = "报表统计 POST请求查询库存统计信息",response = JSONObject.class, notes = "POST请求查询库存统计信息")
    @PostMapping(value="/stockStatistics")
    public @ResponseBody
    JSONObject stockStatistics(@RequestBody InventoryStatistics entity, HttpSession session) {
        Page<InventoryStatistics> list = new Page<InventoryStatistics>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
           list= baseService.stockCount(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (ServiceException e) {
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping(value = "stockStatisticsExport")
    @ApiOperation(value = "导出库存统计信息", response = JSONObject.class, notes = "导出库存统计信息")
    public @ResponseBody void stockStatisticsExport(@RequestBody InventoryStatistics entity, HttpSession session, HttpServletResponse response) throws IOException {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));
        entity.setCkid(ckid);
        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();

        }
        entity.setHzid(hzid);
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("库存统计信息", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<InventoryStatistics> list = baseService.stockCountExport(entity);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, InventoryStatistics.class, list);
        workbook.write(response.getOutputStream());
    }

    @ApiOperation(value = "报表统计 POST请求查询货品发运情况分析",response = JSONObject.class, notes = "POST请求查询货品发运情况分析")
    @PostMapping(value="/shipmentStatistics")
    public @ResponseBody JSONObject shipmentOrderFx(@RequestBody ShipmentOrderFx fx, HttpSession session) {
        Page<ShipmentOrderFx> list = new Page<ShipmentOrderFx>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid= hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= userinfo.getUser().getOrgId().get(0).toString();
           fx.setHzid(hzid);
          fx.setCkid(ckid);
            list=baseService.shipmentOrderFx(fx);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping(value = "shipmentStatisticsExport")
    @ApiOperation(value = "导出货品发运情况分析", response = JSONObject.class, notes = "导出货品发运情况分析")
    public @ResponseBody void shipmentStatisticsExport(@RequestBody ShipmentOrderFx fx, HttpSession session, HttpServletResponse response) throws IOException {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));

        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();

        }
        fx.setCkid(ckid);
        fx.setHzid(hzid);
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("货品发运情况分析", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<ShipmentOrderFx> list = baseService.shipmentOrderFxExport(fx);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, ShipmentOrderFx.class, list);
        workbook.write(response.getOutputStream());
    }
    @ApiOperation(value = "报表统计 POST请求查询当前货主下的货品种类",response = JSONObject.class, notes = "POST请求查询当前货主下的货品种类")
    @PostMapping(value="/goodsType")
    public @ResponseBody JSONObject goodsType(HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid= hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= userinfo.getUser().getOrgId().get(0).toString();
           List<WmsGoodsType> type=baseService.seachHpzl(ckid,hzid);
            json = SetStatus.setSearchType(JSONArray.fromObject(type).toString(),(int) type.size());
        }catch (Exception e) {
            e.printStackTrace();
        }

        return json;
    }
    @ApiOperation(value = "报表统计 POST请求根据货品种类查询货品",response = JSONObject.class, notes = "POST请求根据货品种类查询货品")
    @PostMapping(value="/seachGoods")
    public @ResponseBody JSONObject seachGoods(@RequestBody SetAndModifyModel model, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            List<WmsGoods> type=baseService.seachHpByZlid(model.getIds());
            json = SetStatus.setSearchType(JSONArray.fromObject(type).toString(),(int) type.size());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
//    @ApiOperation(value = "报表统计 POST查询订单渠道",response = JSONObject.class, notes = "POST查询订单渠道")
//    @PostMapping(value="/seachDdqd")
//    public @ResponseBody JSONObject seachDdqd(HttpSession session) {
//        JSONObject json = getJSONObject();
//        try {
//            //获取当前操作用户的货主id
//            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
//            String hzid= hz.get(0).getHzid();
//            //获取仓库Id
//            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
//            String ckid= userinfo.getUser().getOrgId().get(0).toString();
//            List<WmsOrderChannel>  type=baseService.seachDdqd(ckid,hzid);
//            json = SetStatus.setSearchType(JSONArray.fromObject(type).toString(),(int) type.size());
//        }catch (Exception e) {
//            e.printStackTrace();
//        }
//        return json;
//    }
    @ApiOperation(value = "报表统计 POST请求查询品项数分析表头",response = JSONObject.class, notes = "POST请求查询品项数分析表头")
    @PostMapping(value="/goodsTypeStatisticsTitle")
    public @ResponseBody JSONObject goodsTypeStatisticsTitle(@RequestBody ShipmentOrderFx fx) {
        JSONObject json = getJSONObject();
        try {
            json = baseService.seachPxsfxTitle(fx);
        }catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"请求表头失败",e.getMessage(),"");
        }
        return json;
    }
    @ApiOperation(value = "报表统计 POST请求查询品项数分析",response = JSONObject.class, notes = "POST请求查询品项数分析")
    @PostMapping(value="/goodsTypeStatistics")
    public @ResponseBody JSONObject goodsTypeStatistics(@RequestBody ShipmentOrderFx fx, HttpSession session) {
        Page<Map<String,Object>> list = new Page<Map<String,Object>>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid= hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= userinfo.getUser().getOrgId().get(0).toString();
            fx.setCkid(ckid);
            fx.setHzid(hzid);
            list=baseService.seachPxsfx(fx);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping(value = "goodsTypeStatisticsExport")
    @ApiOperation(value = "导出品项数分析", response = JSONObject.class, notes = "导出品项数分析")
    public @ResponseBody void goodsTypeStatisticsExport(@RequestBody ShipmentOrderFx fx, HttpSession session, HttpServletResponse response) throws IOException {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));

        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();

        }
        fx.setCkid(ckid);
        fx.setHzid(hzid);
        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("库存统计信息", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<ExcelExportEntity> title=baseService.seachPxsfxExporttitle(fx);
        List<Map<String,Object>> list = baseService.seachPxsfxExport(fx);
        Workbook workbook = ExcelExportUtil.exportExcel(ep,title, list);
        workbook.write(response.getOutputStream());
    }
    @ApiOperation(value = "报表统计 POST请求查询物流快递报表",response = JSONObject.class, notes = "POST请求查询物流快递报表")
    @PostMapping(value="/seachWlkd")
    public @ResponseBody JSONObject seachWlkd(@RequestBody LogisticsExpress entity, HttpSession session) {
        Page<LogisticsExpress> list = new Page<LogisticsExpress>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String hzid= hz.get(0).getHzid();
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid= userinfo.getUser().getOrgId().get(0).toString();
            list=baseService.seachWlkd(ckid,hzid,entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping(value = "seachWlkdExport")
    @ApiOperation(value = "导出物流快递报表", response = JSONObject.class, notes = "导出物流快递报表")
    public @ResponseBody void seachWlkdExport(@RequestBody LogisticsExpress entity, HttpSession session, HttpServletResponse response) throws IOException {
        //用户信息
        UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        //仓库id
        String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));

        List<SysHz> hz = (List) session.getAttribute(BaseSessionInfo.SessionConsignor);
        String hzid = null;
        if (hz.size() > 0) {
            hzid = hz.get(0).getHzid();

        }

        // 告诉浏览器用什么软件可以打开此文件
        response.setHeader("content-Type", "application/vnd.ms-excel");
        // 下载文件的默认名称
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("物流快递报表", "UTF-8") + ".xls");
        //编码
        response.setCharacterEncoding("UTF-8");
        ExportParams ep = new ExportParams();
        ep.setStyle(ManySheetOneSyler.class);
        List<LogisticsExpress> list = baseService.seachWlkdExport(ckid,hzid,entity);
        Workbook workbook = ExcelExportUtil.exportExcel(ep, LogisticsExpress.class, list);
        workbook.write(response.getOutputStream());
    }
}
